package servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import dao.LoveMusicDao;
import entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


@WebServlet("/removeLoveServlet")
public class RemoveLoveServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");

        String strId = req.getParameter("id");
        int musicid = Integer.parseInt(strId);

        User user = (User)req.getSession().getAttribute("user");
        int userid = user.getId();

        Map<String,Object> map=new HashMap<>();

        LoveMusicDao loveMusicDao = new LoveMusicDao();
        int ret = loveMusicDao.removeLoveMusic(userid,musicid);

        if(ret == 1) {
            map.put("msg",true);
        }else {
            map.put("msg",false);
        }

        ObjectMapper objectMapper = new ObjectMapper();

        objectMapper.writeValue(resp.getWriter(),map);

    }
}

